//
//  LGRecoveryDetailHeadView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/7.
//

#import "LGRecoveryDetailHeadView.h"

#import "LGRecoveryDetailContent.h"
@interface LGRecoveryDetailHeadView()
/**   */
@property (nonatomic , strong)UILabel *topTitleLabel;
/**   */
@property (nonatomic , strong)UIImageView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *timeTitle;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *rateTitle;
/**   */
@property (nonatomic , strong)UILabel *rateLabel;
/**   */
@property (nonatomic , strong)UIView *contentView;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIButton *detailBtn;
/**   */
@property (nonatomic , strong)UILabel *sendTime;
/**   */
@property (nonatomic , strong)UILabel *sendTitle;
/**   */
@property (nonatomic , strong)UILabel *sendCount;
/**   */
@property (nonatomic , strong)UILabel *trainTitle;

@end

@implementation LGRecoveryDetailHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setIsCommon:(BOOL)isCommon{
    _isCommon = isCommon;
    if(isCommon == YES){
        //运动方案-方案详情
        self.topTitleLabel.hidden = YES;
        self.historyBtn.hidden = YES;
        self.sendTime.hidden = YES;
        self.sendTitle.hidden = NO;
        self.sendCount.hidden = NO;
        self.sendRecord.hidden = NO;
        self.baseView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(179));
        self.trainTitle.frame = CGRectMake(viewPix(16), viewPix(250), viewPix(120), 22);
    }else{
        //患者档案-运动方案
        self.topTitleLabel.hidden = NO;
        self.historyBtn.hidden = NO;
        self.sendTime.hidden = NO;
        self.sendTitle.hidden = YES;
        self.sendCount.hidden = YES;
        self.sendRecord.hidden = YES;
        self.baseView.frame = CGRectMake(viewPix(16), viewPix(54), Screen_W-viewPix(32), viewPix(205));
        self.trainTitle.frame = CGRectMake(viewPix(16), viewPix(276), viewPix(120), 22);
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *title = LGNSString(dataDic[@"title"]);
    UIFont *font = [title textWidthWithFont:LGFontWeight(16, UIFontWeightMedium) maxHeight:MAXFLOAT]>(Screen_W-viewPix(60))?LGFontWeight(15, UIFontWeightMedium):LGFontWeight(16, UIFontWeightMedium);
    self.titleLabel.font = font;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.timeLabel.text = [NSString stringWithFormat:@"%@分钟",dataDic[@"duration"]];
    self.rateLabel.text = LGNSString(dataDic[@"frequency"]);
    self.sendTime.text = [NSString stringWithFormat:@"%@ %@发放",dataDic[@"send_date"],dataDic[@"doctor_name"]];
    self.sendCount.text = LGNSString(dataDic[@"send_count"]);
    
    NSString *content = LGNSString(dataDic[@"description"]);
    self.contentLabel.text = [self contentString:content];
    self.detailBtn.hidden = [[self contentString:content] isEqualToString:content]?YES:NO;
    [self.contentLabel lineSpacing:7];
}

//查看详情
-(void)checkContentDetailAction{
    LGRecoveryDetailContent *contentView = [[LGRecoveryDetailContent alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    contentView.title = LGNSString(self.dataDic[@"title"]);
    contentView.content = LGNSString(self.dataDic[@"description"]);
    [[UIApplication sharedApplication].keyWindow addSubview:contentView];
}

-(NSString *)contentString:(NSString *)content{
    CGFloat height = [content textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(72) lineSpacing:7];
    CGFloat labelHeight = viewPix(48);
    if(height>labelHeight){
        for(NSUInteger i =1; i < content.length; i++){
            NSString *subString = [content substringToIndex:i];
            if ([subString textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(72) lineSpacing:7] > labelHeight){
                return [NSString stringWithFormat:@"%@...",[subString substringToIndex:i-5]];
            }
        }
    }
    return content;
}
    
             
    


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topTitleLabel];
    [self addSubview:self.historyBtn];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.timeTitle];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.rateTitle];
    [self.baseView addSubview:self.rateLabel];
    [self.baseView addSubview:self.contentView];
    [self.baseView addSubview:self.sendTime];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.detailBtn];
    [self addSubview:self.sendTitle];
    [self addSubview:self.sendCount];
    [self addSubview:self.sendRecord];
    [self addSubview:self.trainTitle];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(8));
        make.right.equalTo(self.contentView).offset(-viewPix(8));
    }];
}

-(UILabel *)topTitleLabel{
    if(!_topTitleLabel){
        _topTitleLabel = [UILabel lableWithText:@"患者当前运动方案" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _topTitleLabel.frame = CGRectMake(viewPix(17), viewPix(16), viewPix(150), viewPix(22));
        _topTitleLabel.hidden = YES;
    }
    return _topTitleLabel;
}

-(UIButton *)historyBtn{
    if(!_historyBtn){
        _historyBtn = [UIButton buttonWithTitle:@"历史方案" titleFont:LGFont(15) textColor:@"#666666" imageName:@"grayTip12"];
        _historyBtn.frame = CGRectMake(Screen_W-viewPix(95), viewPix(16), viewPix(80), viewPix(22));
        [_historyBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        _historyBtn.hidden = YES;
    }
    return _historyBtn;
}

-(UIImageView *)baseView{
    if(!_baseView){
        _baseView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"运动方案-详情背景"]];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(179));
        _baseView.userInteractionEnabled = YES;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(60), viewPix(22));
    }
    return _titleLabel;
}

-(UIButton *)timeTitle{
    if(!_timeTitle){
        _timeTitle = [UIButton buttonWithTitle:@"时长" titleFont:LGFont(15) textColor:@"#FFFFFF" imageName:@"运动方案-时长"];
        _timeTitle.frame = CGRectMake(viewPix(16), kMaxY(self.titleLabel.frame)+viewPix(10), viewPix(60), viewPix(21));
        _timeTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _timeTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
        _timeTitle.userInteractionEnabled = NO;
    }
    return _timeTitle;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _timeLabel.frame = CGRectMake(viewPix(16), kMaxY(self.timeTitle.frame)+viewPix(2), viewPix(90), viewPix(21));
    }
    return _timeLabel;
}

-(UIButton *)rateTitle{
    if(!_rateTitle){
        _rateTitle = [UIButton buttonWithTitle:@"频率" titleFont:LGFont(15) textColor:@"#FFFFFF" imageName:@"运动方案-频率"];
        _rateTitle.frame = CGRectMake(viewPix(10)+(Screen_W-viewPix(32))/2.0, kMinY(self.timeTitle.frame), viewPix(60), viewPix(21));
        _rateTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _rateTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
        _rateTitle.userInteractionEnabled = NO;
    }
    return _rateTitle;
}

-(UILabel *)rateLabel{
    if(!_rateLabel){
        _rateLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _rateLabel.frame = CGRectMake(kMinX(self.rateTitle.frame), kMinY(self.timeLabel.frame), viewPix(170), viewPix(21));
    }
    return _rateLabel;
}

-(UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(12), viewPix(103), Screen_W-viewPix(56), viewPix(64))];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.cornerRidus = 5;
    }
    return _contentView;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:2];
//        _contentLabel.frame = CGRectMake(viewPix(8), viewPix(8), Screen_W-viewPix(72), viewPix(48));
    }
    return _contentLabel;
}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"详情" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"blackTip16"];
        _detailBtn.frame = CGRectMake(self.contentView.size.width-viewPix(64), viewPix(34), viewPix(58), viewPix(21));
        _detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _detailBtn.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        [_detailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_detailBtn addTarget:self action:@selector(checkContentDetailAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

-(UILabel *)sendTime{
    if(!_sendTime){
        _sendTime = [UILabel lableWithText:@"发送患者:" colorString:@"#FFFFFF" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _sendTime.frame = CGRectMake(viewPix(16), kMaxY(self.contentView.frame)+viewPix(8), Screen_W-viewPix(56), viewPix(19));
    }
    return _sendTime;
}

-(UILabel *)sendTitle{
    if(!_sendTitle){
        _sendTitle = [UILabel lableWithText:@"发送患者:" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _sendTitle.frame = CGRectMake(viewPix(16), kMaxY(self.baseView.frame)+viewPix(12), 70, viewPix(22));
    }
    return _sendTitle;
}

-(UILabel *)sendCount{
    if(!_sendCount){
        _sendCount = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _sendCount.frame = CGRectMake(kMaxX(self.sendTitle.frame)+viewPix(12), kMinY(self.sendTitle.frame), viewPix(150), viewPix(22));
    }
    return _sendCount;
}

-(UIButton *)sendRecord{
    if(!_sendRecord){
        _sendRecord = [UIButton buttonWithTitle:@"发放记录" titleFont:LGFont(15) textColor:@"#666666" imageName:@"grayTip12"];
        _sendRecord.frame = CGRectMake(Screen_W-viewPix(95), kMaxY(self.baseView.frame)+viewPix(12), viewPix(80), viewPix(22));
        [_sendRecord setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
    }
    return _sendRecord;
}

-(UILabel *)trainTitle{
    if(!_trainTitle){
        _trainTitle = [UILabel lableWithText:@"训练目录" colorString:@"#000000" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _trainTitle.frame = CGRectMake(viewPix(16), viewPix(250), viewPix(120), 22);
    }
    return _trainTitle;
}

@end
